package com.course.controller;

import com.course.convert.StrToListFormatter;
import com.course.entity.dto.CourseDto;
import com.course.service.ICampusCourseService;
import com.easyCode.feature.mybaits.CustomPage;
import com.easyCode.feature.result.ResponseResultAdvice;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.DataBinder;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Positive;
import java.util.List;

@Validated
@RestController
@RequestMapping("/campusCourse")
@RequiredArgsConstructor
@ResponseResultAdvice
@Api(tags = "主校区课程请求")
public class CampusCourseController {
    private final ICampusCourseService iCampusCourseService;
    
    @InitBinder
    public void initBinder(DataBinder dataBinder){
        dataBinder.addCustomFormatter(new StrToListFormatter());
    }

    @PostMapping("/realse/{cId}")
    @ApiOperation("分配课程给某个主校区")
    public void releaseCoursesToCampus(@Positive(message = "非法参数") @PathVariable("cId")Integer cId,
                                       @ApiParam("课程id列表,由'-'分割课程id,如: 1-2-3") @RequestParam("courseIdList")List<Integer> courseIdList){
        iCampusCourseService.releaseCoursesToCampus(cId,courseIdList);
    }

    @PostMapping("/revoke/{cId}")
    @ApiOperation("回收分配给主校区的课程")
    public void revokeCoursesFromCampus(@Positive(message = "非法参数") @PathVariable("cId")Integer cId,
                                         @ApiParam("课程id列表,由'-'分割课程id,如: 1-2-3") @RequestParam("courseIdList")List<Integer> courseIdList){
        iCampusCourseService.revokeCoursesFromCampus(cId,courseIdList);
    }

    @PostMapping("/list/{cId}")
    @ApiOperation("查询分配给主校区的课程")
    public List<CourseDto> listCoursesReleaseToCampus(@Positive(message = "非法参数") @PathVariable("cId")Integer cId, CustomPage customPage){
        return iCampusCourseService.listCoursesReleaseToCampus(cId,customPage);
    }

}
